/**
 * connect db
 * export db connect
 */
const CONFIG = require('./config');
const MONGOOSE = require('mongoose');

// mongo connect url
const CONNECT_URL = `${CONFIG.protocol}${CONFIG.user}:${CONFIG.pwd}@${CONFIG.host}:${CONFIG.port}/${CONFIG.dbname}`;

// when mongo connected will show msg.
MONGOOSE.connection.on('connected', () => {
    console.log('mongo db connected');
});

// connect
MONGOOSE.connect(CONNECT_URL);

// table
const tables = {
    userCenter: {
        userName: {
            type: String,
            required: true,
        },
        password: {
            type: String,
            required: true,
        },
        userType: {
            type: Number,
            required: true,
        },
        tx: {
            type: String
        },
        jj: {
            type: String
        },
        zw: {
            type: String
        },
        m: {
            type: String
        }
    },
    chat: {
        from: {
            type: String,
            required: true
        },
        to: {
            type: String,
            required: true
        },
        msg: {
            type: String,
            default: ''
        },
        create_time: {
            type: String,
            default: new Date().getTime()
        }, 
        chat_id: {
            type: String,
            required: true
        },
        isRead: { 
            type: Boolean,
            default: false
        },
    }
};
// create table
for(let tableName in tables){
    MONGOOSE.model(tableName, MONGOOSE.Schema(tables[tableName]));
}

const getTable = (table = '') => {
    return MONGOOSE.model(table);
};

module.exports = getTable;
